package com.zh.api.controller.operation;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.domain.DictDO;
import com.zh.common.utils.*;
import com.zh.operation.domain.OperationGoodsBuyDO;
import com.zh.operation.domain.OperationGoodsDO;
import com.zh.operation.service.OperationGoodsBuyService;
import com.zh.operation.service.OperationGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Controller
@Api("资产【养老院物品采购表】")
public class OperationGoodsBuyController extends BaseController {
	@Autowired
	private OperationGoodsBuyService operationGoodsBuyService;
	@Autowired
	private OperationGoodsService operationGoodsService;

	@Log("物品采购信息")
	@ResponseBody
	@RequestMapping(value = "listOperationGoodsBuy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationGoodsBuy(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
        Query query = new Query(params);
		List<OperationGoodsBuyDO> goodsBuyList = operationGoodsBuyService.list(query);
		int total = operationGoodsBuyService.count(query);
		PageUtils pageUtils = new PageUtils(goodsBuyList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("编辑物品采购")
	@ResponseBody
	@RequestMapping(value = "editOperationGoodsBuyById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationGoodsBuyById(@RequestBody OperationGoodsBuyDO goodsBuy){
		if(goodsBuy==null || CheckEmptyUtil.isEmpty(goodsBuy.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		OperationGoodsBuyDO operationGoodsBuyDO = operationGoodsBuyService.get(goodsBuy.getId());
	    return ResultVO.success(operationGoodsBuyDO);
	}

	@Log("新增物品采购")
	@ResponseBody
	@RequestMapping(value = "saveOperationGoodsBuy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationGoodsBuy(@RequestBody OperationGoodsBuyDO goodsBuy){
		if(goodsBuy==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		goodsBuy.setCreateBy(getUserId());
		OperationGoodsDO operationGoodsDO = operationGoodsService.isExist(goodsBuy.getGoodsName());
		if(operationGoodsDO != null)
		{
			operationGoodsDO.setGoodsNum(String.valueOf((Integer.valueOf(operationGoodsDO.getGoodsNum())+Integer.valueOf(goodsBuy.getBuyNum()))));
			operationGoodsService.update(operationGoodsDO);
			if(operationGoodsBuyService.save(goodsBuy)>0){
				return ResultVO.success();
			}
		}
		else
		{
			OperationGoodsDO goods = new OperationGoodsDO();
			goods.setId(new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()) + (int) ((Math.random() * 9 + 1) * 1000));
			goods.setCreateBy(getUserId());
			goods.setUpdateBy(getUserId());
			goods.setUpdateDate(new Date());
			goods.setCreateDate(new Date());
			goods.setDelFlag("0");
			goods.setGoodsName(goodsBuy.getGoodsName());
			goods.setGoodsType(goodsBuy.getGoodsType());
			goods.setGoodsNum(goodsBuy.getBuyNum());
			operationGoodsService.save(goods);
			if(operationGoodsBuyService.save(goodsBuy)>0){
				return ResultVO.success();
			}
		}
		return ResultVO.error();
	}
	@Log("更新物品采购")
	@ResponseBody
	@RequestMapping(value = "updateOperationGoodsBuyById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationGoodsBuyById(@RequestBody OperationGoodsBuyDO goodsBuy){
		if(goodsBuy==null || CheckEmptyUtil.isEmpty(goodsBuy.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		operationGoodsBuyService.update(goodsBuy);
		return ResultVO.success();
	}

	@Log("删除物品采购")
	@ResponseBody
	@RequestMapping(value = "removeOperationGoodsBuyById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationGoodsBuyById( @RequestBody OperationGoodsBuyDO goodsBuy){
		if(goodsBuy==null || CheckEmptyUtil.isEmpty(goodsBuy.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(operationGoodsBuyService.remove(goodsBuy.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}

	// TODO: 2019/6/15 获取未知类型
	@Log("获取类型")
	@ResponseBody
	@RequestMapping(value = "getType", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO getType(){
		List<DictDO> type= operationGoodsBuyService.getType();
		return ResultVO.success(type);
	}
	
//	/**
//	 * 删除
//	 */
//	@PostMapping( "/batchRemove")
//	@ResponseBody
//	@ApiOperation("批量删除")
//	public ResultVO remove(@RequestParam("ids[]") String[] ids){
//		operationGoodsBuyService.batchRemove(ids);
//		return ResultVO.success();
//	}
    @RequestMapping(value = "batchRemoveOperationGoodsBuyByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation("批量删除物品采购")
    public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
    	if(jsonParam==null){
    		return ResultVO.error(ResultEnum.PARAME_ERROR);
    	}
    	List<String> ids= (List<String>)jsonParam.get("ids");
    	if(CheckEmptyUtil.isEmpty(ids)){
    		return ResultVO.error(ResultEnum.PARAME_ERROR);
    	}
    	String[] strs1=ids.toArray(new String[ids.size()]);
    	if(operationGoodsBuyService.batchRemove(strs1)>0){
    		return ResultVO.success();
    	}
    	return ResultVO.error();
    }
}
